﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Negocios;

public partial class Manter : System.Web.UI.Page
{
    LocalBLL local = new LocalBLL();

    protected void Page_Load(object sender, EventArgs e)
    {
        Session["usuario"] = "Teste";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        tbNome.Enabled = true;
        tbLocalizacao.Enabled = true;
        btCancelar.Enabled = true;
        btGravar.Enabled = true;
        btNovo.Enabled = false;
    }

    private void cancelar()
    {
        tbCodigo.Text = "";
        tbNome.Text = "";
        tbLocalizacao.Text = "";
        tbNome.Enabled = false;
        btGravar.Enabled = false;
        tbLocalizacao.Enabled = false;
        btCancelar.Enabled = false;
        btNovo.Enabled = true;
    }

    private void limpar()
    {
        tbCodigo.Text = "";
        tbNome.Text = "";
        tbLocalizacao.Text = "";
        lbMensagem.Text = "";
    }

    protected void btGravar_Click(object sender, EventArgs e)
    {
        if(tbCodigo.Text == "")
        {
            try
            {
                Local l = new Local(tbNome.Text,tbLocalizacao.Text,Session["usuario"].ToString());
                local.Insere(l);
            }
            catch (Exception ex)
            {
                lbMensagem.Text = ex.Message;
            }
        }
        else
        {
            local.Atualiza(new Local(Convert.ToInt32(tbCodigo.Text), tbNome.Text, tbLocalizacao.Text,Session["usuario"].ToString()));
        }

        limpar();
        GridView1.DataBind();
        btNovo.Enabled = true;

    }
    protected void btCancelar_Click(object sender, EventArgs e)
    {
        cancelar();
    }
    protected void cmdRemover_Click(object sender, EventArgs e)
    {
        int codigo = Convert.ToInt32(((GridViewRow)((DataControlFieldCell)((LinkButton)sender).Parent).Parent).Cells[0].Text);

        try
        {
            Local l = new Local(codigo);
            local.Remove(l);
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            lbMensagem.Text = ex.Message;
        }
    }
    protected void cmdAtualiza_Click(object sender, EventArgs e)
    {
        int codigo = Convert.ToInt32(((GridViewRow)((DataControlFieldCell)((LinkButton)sender).Parent).Parent).Cells[0].Text);

        try
        {
            LocalBLL localBLL = new LocalBLL();
            Local local = localBLL.consulta(codigo);
            tbCodigo.Text = Convert.ToString(local.Codigo);
            tbNome.Text = local.Nome;
            tbLocalizacao.Text = local.Localizacao;
        }
        catch (Exception ex)
        {
            lbMensagem.Text = ex.Message;
        }

       

        tbNome.Enabled = true;
        tbLocalizacao.Enabled = true;
        btCancelar.Enabled = true;
        btGravar.Enabled = true;
        btNovo.Enabled = false;
       
    }
}
